package my.merborn.ordersysteam105.models.servicesImpl;

import my.merborn.ordersysteam105.models.dao.MenuMapper;
import my.merborn.ordersysteam105.models.dao.OrderMapper;
import my.merborn.ordersysteam105.models.dao.jpa.OrderMapperJPA;
import my.merborn.ordersysteam105.models.pojo.Menu;
import my.merborn.ordersysteam105.models.pojo.OrderT;
import my.merborn.ordersysteam105.models.pojo.User;
import my.merborn.ordersysteam105.models.pojo.enums.FinishStatus;
import my.merborn.ordersysteam105.models.pojo.enums.PayStatus;
import my.merborn.ordersysteam105.models.services.MenuService;
import my.merborn.ordersysteam105.models.services.OrderService;
import my.merborn.ordersysteam105.models.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    UserService us;

    @Autowired
    MenuService ms;

    @Autowired
    MenuMapper mm;

    @Autowired
    OrderMapper om;

    @Autowired
    OrderMapperJPA omj;

    @Override
    public OrderT createNewOrder(User user, String data) {
        if(user.getuId()==null||data==null)return null;
        OrderT order = new OrderT();
        order.setuId(user.getuId());
        order.setMenuList(data);    //保存菜单
        order.setoDate(new Date().toString()); //存放当前时间
        order.setAmountPrice(getPrice(data));
        order.setDiscount(1d);
        order.setActualPayment(order.getAmountPrice()*order.getDiscount());
        order.setIsfinished(FinishStatus.NOT_FINISH.getStatus());
        order.setIspayed(PayStatus.NO_PAY.getStatus());
        om.insertSelective(order);
        ms.incrementMenuCountByOrderList(data); //将点的菜增加count
        return order;
    }

    @Override
    public List<OrderT> selectALL() {
        List<OrderT> orderTS = om.selectALL();
        return orderTS;
    }

    @Override
    public List<OrderT> selectNoPayed(Integer uid) {
        List<OrderT> orderTS = om.selectNoPayed(uid);
        return orderTS;
    }

    @Override
    public List<OrderT> selectByPageable(Pageable pageable) {
        Page<OrderT> all = omj.findAll(pageable);
        List<OrderT> content = all.getContent();
        return content;
    }

    @Override
    public List<OrderT> selectNoPayedAndNoFinishedByPageable(Pageable pageable) {
        Page<OrderT> all = omj.findOrderTSByIsfinishedAndIspayed(FinishStatus.NOT_FINISH.getStatus(), PayStatus.NO_PAY.getStatus(), pageable);
        List<OrderT> content = all.getContent();
        return content;
    }

    @Override
    public int updateOrder(OrderT order, String data) {
        OrderT o = om.selectByPrimaryKey(order.getoId());
        o.setMenuList(data);
        om.updateByPrimaryKeySelective(o);
        return 0;
    }

    @Override
    public int dropOrder(OrderT order) {
        om.deleteByPrimaryKey(order.getoId());
        return 0;
    }

    @Override
    public double payIt(OrderT order) {
        OrderT o = om.selectByPrimaryKey(order.getoId());
        o.setIspayed(PayStatus.PAY.getStatus());
        om.updateByPrimaryKeySelective(o);
        return 0;
    }

    @Override
    public boolean isPayed(OrderT order) {
        OrderT o = om.selectByPrimaryKey(order.getoId());
        if(o.getIspayed().equals(PayStatus.PAY.getStatus())){
            return true;
        }
        return false;
    }

    @Override
    public List<OrderT> selectNoPayedAndNoFinished() {
        List<OrderT> list = om.selectNoPayedAndNoFinished();
        return list;
    }

    @Override
    public int finishIt(OrderT order) {
        OrderT o = om.selectByPrimaryKey(order.getoId());
        o.setIsfinished(FinishStatus.FINISH.getStatus());
        return om.updateByPrimaryKeySelective(o);
    }




    public double getPrice(String data){
        double price = 0;
        String[] split = data.split("@");
        for (String s : split) {
            Menu food = mm.selectByPrimaryKey(Integer.parseInt(s));
            price += food.getmPrice();
        }
        return price;
    }
}
